package com.topscore.uke.mvp.presenters;

import com.topscore.uke.entities.MemberCommentsInfo;
import com.topscore.uke.mvp.contractors.GetMemberCommentsContractor;
import com.topscore.uke.mvp.model.GetMemberCommentsModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.network.request.GetMemberCommentsRequest;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;
import com.topscore.uke.utils.UserInfoManager;

public class GetMemberCommentsPresenter implements GetMemberCommentsContractor, GetMemberCommentsContractor.Presenter {
    private View mView;
    private GetMemberCommentsRequest mRequest;

    private int mCurrentPage = 1;

    private final int _PageSize = 15;

    public GetMemberCommentsPresenter(View view) {
        this.mView = view;
        mRequest = new GetMemberCommentsRequest();
        mRequest.setEmpNum(UserInfoManager.getUserInfo().getLoginName());
        mRequest.setPageParams(new GetMemberCommentsRequest.PageParams(_PageSize, 1));
    }

    @Override
    public void getComment() {
        mView.getLoadService().showCallback(LoadingCallBack.class);
        new GetMemberCommentsModel(new Model.CallBack() {
            @Override
            public void onSuccess(BaseResponse<MemberCommentsInfo> response) {
                mView.getRecyclerAdapter().setNewData(response.getData().getDataList());
                if (!response.getData().getDataList().isEmpty()
                        && response.getData().getDataList().size() < _PageSize) {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.getLoadService().showCallback(ErrorCallBack.class);
            }

            @Override
            public void onComplete() {
                mView.getLoadService().showSuccess();
            }
        }).getComments(mRequest);
    }

    @Override
    public void loadNextPage() {
        int page = mCurrentPage + 1;
        mRequest.setPageParams(new GetMemberCommentsRequest.PageParams(15, page));
        new GetMemberCommentsModel(new Model.CallBack() {
            @Override
            public void onSuccess(BaseResponse<MemberCommentsInfo> response) {
                if (response.getData().getDataList() != null
                        && !response.getData().getDataList().isEmpty()) {
                    mCurrentPage = response.getData().getCurrentPage();
                    mView.getRecyclerAdapter().addData(response.getData().getDataList());
                    if (response.getData().getDataList().size()<_PageSize){
                        mView.getRecyclerAdapter().loadMoreComplete();
                        mView.getRecyclerAdapter().loadMoreEnd();
                    }
                } else {
                    mView.getRecyclerAdapter().loadMoreComplete();
                    mView.getRecyclerAdapter().loadMoreEnd();
                }

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.getRecyclerAdapter().loadMoreFail();
            }

            @Override
            public void onComplete() {
                mView.getRecyclerAdapter().loadMoreComplete();
            }
        }).getComments(mRequest);
    }
}
